package com.bank.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bank.code.RespCode;
import com.bank.dao.TellerDao;
import com.bank.entity.Teller;
import com.bank.excption.CommonException;
import com.bank.util.CommonUtil;

@Service(value="tellerService")
public class TellerServiceImpl implements TellerService{

	@Autowired
	TellerDao tellerDao;

	public List<Object> getTellerList(Map<String, Object> param) {
		return tellerDao.getTellerList(param);
	}

	public Integer getTellerListCount(Map<String, Object> param) {
		return tellerDao.getTellerListCount(param);
	}

	@Transactional
	public void updateTellerInfo(Map<String, Object> params) throws CommonException {
		String number = (String)params.get("teller_number");
		String teller_name = (String)params.get("teller_name");
		String teller_role = (String)params.get("teller_role");
		String teller_status = (String)params.get("teller_status");
		String tellerphone = (String)params.get("tellerphone");
		String teller_email = (String)params.get("teller_email");
		Teller teller = tellerDao.getTellerByNumber(number);
		if(teller==null){
			throw new CommonException(RespCode.SYSTEM_FAILUE, "系统错误");
		}
		if(teller_name!= null && teller_name.equals("")==false){
			//password = CommonUtil.passwordEncryption(password);
			teller.setTeller_name(teller_name);
		}
		if(teller_role!=null && teller_role.equals("")== false){
			teller.setTeller_role(teller_role);
		}
		if(teller_status!=null && teller_status.equals("")==false){
			teller.setTeller_status(teller_status);
		}
		if(tellerphone!=null && tellerphone.equals("")==false){
			teller.setTellerphone(tellerphone);
		}
		if(teller_email!=null && teller_email.equals("")==false){
			teller.setTeller_email(teller_email);
		}
		tellerDao.updateTellerInfo(teller);
	}

	public Teller getTellerByNumber(String number) {
		return tellerDao.getTellerByNumber(number);
	}

	public void addTeller(Map<String, Object> params) throws CommonException {
		// TODO Auto-generated method stub
		String number = (String)params.get("teller_number");
		String teller_name = (String)params.get("teller_name");
		String teller_password = (String)params.get("teller_password");
		String teller_role = (String)params.get("teller_role");
		String tellerphone = (String)params.get("tellerphone");
		String teller_email = (String)params.get("teller_email");
		String teller_id_type = (String)params.get("teller_id_type");
		String teller_idno = (String)params.get("teller_idno");
		String organization = (String)params.get("organization");
		Teller teller = null;
		/*teller  = tellerDao.getTellerByNumber(number);
		if(teller != null){
			throw new CommonException(RespCode.TELLER_IS_EXIST, "柜员代号已存在");
		}*/
		teller = null;
		teller = new Teller();
		teller.setTeller_number(number);
		teller.setTeller_name(teller_name);
		teller.setTeller_password(CommonUtil.passwordEncryption(teller_password));
		teller.setTeller_role(teller_role);
		teller.setTeller_status("0");
		teller.setTellerphone(tellerphone);
		teller.setTeller_email(teller_email);
		teller.setTeller_id_type(teller_id_type);
		teller.setTeller_idno(teller_idno);
		teller.setOrganization(organization);
		tellerDao.save(teller);
	}
	
	

	
}
